<template>
  <!-- 最大盒子 -->
  <s-layout :tabbarshow="false" id="layout" navbar="normal" :title="''" :autoBack="true" leftIconColor="$uni-text-color" :bgColor="'rgba(0,0,0,0)'">
    <!-- 使用web-view组件加载PDF.js -->
    <web-view :src="url" @message="getMessage($event,name)"></web-view>
  </s-layout>
</template>
<script setup>
  import sheep from '@/sheep';
  import { ref, reactive } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import { baseUrl } from "@/sheep/config";
  import { isEmpty } from 'lodash';
  const name = ref('enrollment')
  const C_department_id = ref()
  const url = ref();
  const introduction = reactive({
    contract: '',
    id:'',
    share_id:''
  })
  const paramss = ref()
  onLoad((options) => {
    paramss.value = JSON.parse(options.params);
    C_department_id.value = options.department_id
    name.value = options.name
    introduction.contract = options.url
    introduction.id = options.id
    if (options.url) {
      url.value = baseUrl + '/uploads/pdf-h5-master/pdfH5.html?contract=' + encodeURIComponent(options.url)
    } else {
      request_contact()
    }
  })
  const seal = ref()
  const getMessage = (e,a) => {
    seal.value = e.detail.data[0].image.replace(/data.+?;base64,/, "")
      if(a == 'enrollment'){
        setTimeout(async() => {
          let params = {
            contract:introduction.contract,
            department_id:paramss.value.department_id,
            seal:seal.value,
            is_contact:paramss.value.is_contact,
            id:introduction.id,
            share_id:introduction.share_id
          }
          // 入职申请（2-个人签字）
          const res = await sheep.$api.dispatch.enrollmentSignature(params)
          uni.showLoading({ title: '加载中' });   
          if(res.code == 1){
            if(paramss.value.is_contact == 1){
              request_contact()
            } else {
              uni.hideLoading()
              uni.showModal({
                    title: '提示',
                    content: res.msg,
                    showCancel: false,
                      success: function (results) {
                          if (results.confirm) {
                            sheep.$store('user').getInfo();
                            sheep.$router.go('/pages/user/user/user')
                          } 
                      }
                  });
            }
          }else{
            uni.showModal({
                    title: '提示',
                    content: res.msg,
                    showCancel: false,
                      success: function (results) {
                          if (results.confirm) {
                            sheep.$store('user').getInfo();
                            sheep.$router.go('/pages/user/user/user')
                          } 
                      }
                  });
          }
        },500)
      
        
      }else if(a == 'contact'){
       
        setTimeout(async() => {
        let paramData ={
              contract: introduction.contract,
              seal:seal.value,
              department_id:C_department_id.value,
              id:introduction.id,
              share_id:introduction.share_id
            }
            const ress = await sheep.$api.dispatch.contactSignature(paramData)
               if(ress.code == 1){
                  uni.showModal({
                  title: '提示',
                  content: ress.msg,
                  showCancel: false,
                    success: function (results) {
                        if (results.confirm) {
                          sheep.$store('user').getInfo();
                          sheep.$router.go('/pages/user/user/user')
                        } 
                    }
                });
                uni.hideLoading()
            }else{
              uni.hideLoading()
              sheep.$helper.toast(ress.msg);
            }
          },500)
      }

      
  
    
  }


  const request_contact = async () =>   {
    let data ={
                hukou_type: paramss.value.hukou_type,
                address: paramss.value.address,
                emergency_contact: paramss.value.emergency_contact,
                emergency_contact_mobile: paramss.value.emergency_contact_mobile,
                company: paramss.value.company,
                probation_start: paramss.value.probation_start,
                probation_end: paramss.value.probation_end,
                city: paramss.value.city,
                position: paramss.value.position,
                wage_rates: paramss.value.wage_rates,
                salary: paramss.value.salary,
                year: paramss.value.year,
                department_name: paramss.value.department.name,
            }
            //入职申请（3-请求劳务合同）
            const result = await sheep.$api.dispatch.requestContact(data)
            if(result.code == 1){
              introduction.contract = result.data.url
              sheep.$router.redirect('/subpackage/user/user/labor_dispatch_pdf',{
                url:result.data.url,
                params:JSON.stringify(paramss),
                entry:JSON.stringify(paramss.value),
                department_id:paramss.value.department_id,
                name: 'contact',
                id:introduction.id,
                share_id:introduction.share_id
                })
            } else {
              uni.hideLoading()
              uni.showModal({
                    title: '提示',
                    content: result.msg,
                    showCancel: false,
                      success: function (results) {
                          if (results.confirm) {
                            sheep.$store('user').getInfo();
                            sheep.$router.go('/pages/user/user/user')
                          } 
                      }
                  });
            }
            // sheep.$helper.toast(result.msg);
  }  
  
  </script>